package com.vision.shop.util;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Properties;

import com.vision.shop.dao.IFactoryDao;
import com.vision.shop.model.ShopDi;

public class DaoUtil {
	
	
	/**
	 * 用反射来创建IFactoryDao
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static IFactoryDao createDaoFactory(){
		IFactoryDao f=null;
		try {
			Properties prop=PropertiesUtil.getDaoProp(); 
			String fs=prop.getProperty("factory");
			Class clz=Class.forName(fs);
			String mn="getInstance";
			Method m=clz.getMethod(mn);
			f=(IFactoryDao)m.invoke(clz);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NoSuchMethodException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SecurityException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return f;
	}
	/*第一个版本没有基于Annotation
	 * public static void diObj(Object obj){
		Method[] ms=obj.getClass().getDeclaredMethods();
		for(Method m:ms){
			String mn=m.getName();
			if(mn.startsWith("set")){
				mn=mn.substring(3);
				mn=mn.substring(0, 1).toLowerCase()+mn.substring(1);
				Object o=DaoUtil.createDaoFactory().getDao(mn);
				try {
					m.invoke(obj, o);
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		
		}
	}*/
	public static void diObj(Object obj) {
		try {
			/**
			 * 获取所有的方法
			 */
			Method[] ms = obj.getClass().getDeclaredMethods();
			for(Method m:ms) {
				/**
				 * 判断方法上面是否有ShopDi的Annotation，有这个Annotation才进行注入
				 */
				if(m.isAnnotationPresent(ShopDi.class)) {
					/*
					 * 获取method上的ShopDi对象
					 */
					ShopDi sd = m.getAnnotation(ShopDi.class);
					/**
					 * 获取这个Annotation的值
					 */
					String mn = sd.value();
					/**
					 * 判断shopDi的value是否为空，如果为空，就等于要使用setXXX这个方法名称
					 * 来完成注入
					 */
					if(mn==null||"".equals(mn.trim())) {
						mn = m.getName().substring(3);
						mn = mn.substring(0,1).toLowerCase()+mn.substring(1);
					}
					Object o = DaoUtil.createDaoFactory().getDao(mn);
					m.invoke(obj, o);
				}
			}
		} catch (SecurityException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
	}
	
}
